package com.pc.admin.service.ipml;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pc.admin.dto.DaDaExcelDTO;
import com.pc.admin.dto.DaDaSearchDTO;
import com.pc.admin.entity.DaDa;
import com.pc.admin.service.DaDaService;
import com.pc.admin.util.CollectionUtils;
import com.pc.admin.util.CommonResult;
import com.pc.admin.util.ExcelUtils;
import com.pc.admin.vo.DaDaVO;
import com.pc.admin.mapper.DaDaMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @Author : ZJ
 * @create 2023/3/11 20:57
 */
@Slf4j
@Service
public class DaDaServiceImpl implements DaDaService {

    @Autowired
    private DaDaMapper daDaMapper;


    /**
     * 增加、编辑
     *
     * @param daDaVO
     * @return
     */
    @Override
    public CommonResult addDaDa(DaDaVO daDaVO) {
        //新创建DaDa对象，将daDaVO的内容复制给daDa实体类中
        DaDa daDa = new DaDa();
        BeanUtils.copyProperties(daDaVO, daDa);
        //判读id是否为空 是新增 否编辑
        if (daDaVO.getId() == null) {
            //对增加的字段进行空判断及校验。（将空判断抽成方法调用即可）
            if (myNullOpinion(daDaVO) != null) {
                return CommonResult.fail(myNullOpinion(daDaVO));
            }
            //设置查询条件效验电话号码是否重复
            LambdaQueryWrapper<DaDa> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(StrUtil.isNotEmpty(daDaVO.getTel()), DaDa::getTel, daDaVO.getTel());
            List<DaDa> daDas = daDaMapper.selectList(wrapper);
            if (CollectionUtil.isNotEmpty(daDas)) {
                return CommonResult.fail("电话号码已经存在，请勿重复添加");
            }
            daDaMapper.insert(daDa);
        } else {
            //编辑  根据id查询到数据后就编辑
            DaDa byId = daDaMapper.selectById(daDaVO.getId());
            BeanUtils.copyProperties(daDaVO, byId);
            daDaMapper.updateById(byId);
            return CommonResult.ok(daDa);
        }
        return CommonResult.ok(daDa);
    }


    /**
     * 列表查询
     *
     * @return
     */
    @Override
    public List<DaDa> queryDaDaList() {
        DaDaVO daDaVO = new DaDaVO();
        LambdaQueryWrapper<DaDa> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(daDaVO.getName()), DaDa::getName, daDaVO.getName());
        wrapper.like(StrUtil.isNotEmpty(daDaVO.getBank()), DaDa::getBank, daDaVO.getBank());
        wrapper.like(StrUtil.isNotEmpty(daDaVO.getTel()), DaDa::getTel, daDaVO.getTel());
        List<DaDa> daDaList = daDaMapper.selectList(wrapper);
        return daDaList;
    }


    /**
     * 详情查询
     *
     * @return
     */
    @Override
    public DaDaVO detailsDaDa(Integer id) {
        return daDaMapper.detailsDaDa(id);
    }


    /**
     * 删除
     *
     * @return
     */
    @Override
    public CommonResult delDaDa(Integer id) {
        DaDa daDa = daDaMapper.queryBylist(id);
        if (daDa.getStatus() != null && daDa.getStatus() == 0) {
            return CommonResult.fail("正在运行中，不能删除");
        }
        daDaMapper.deleteById(daDa);
        return CommonResult.ok();
    }


    /**
     * 导出
     *
     * @return
     */
    @Override
    public void exportExcel(DaDaExcelDTO excelDTO, HttpServletResponse response) {
        List<DaDaVO> resultList;
        List<String> titleList = excelDTO.getTitleList();
        List<String> titleListNames = excelDTO.getTitleListNames();
        String fileName = excelDTO.getFileName();
        if (CollectionUtils.listNotNull(excelDTO.getIds())) {
            DaDaSearchDTO dto = new DaDaSearchDTO();
            dto.setIds(excelDTO.getIds());
            resultList = daDaMapper.queryList(dto);
        } else {
            DaDaSearchDTO dto = new DaDaSearchDTO();
            BeanUtils.copyProperties(excelDTO, dto);
            resultList = daDaMapper.queryList(dto);
        }
        //导出加序号
        int count = 0;
        for (DaDaVO daDaVO : resultList) {
            count++;
            daDaVO.setRowNum(count);
        }
        //导出
//        ExcelUtils.exportExcel(response, titleList, titleListNames, fileName, resultList);
    }


    //对判空作方法抽取，使其代码简化
    private String myNullOpinion(DaDaVO daDaVO) {
        String msg = null;
        //得出经验：判断时不能用！=null 对字符进行判断，很有可能 会报错以及乱报。
        //必须进行工具类判断。StrUtil.isBlank()
        if (StrUtil.isBlank(daDaVO.getName())) {
            msg = "名字不能为空";
        }
        if (StrUtil.isBlank(String.valueOf(daDaVO.getAge()))) {
            msg = "年龄不能为空";
        }
        if (StrUtil.isBlank(daDaVO.getGender())) {
            msg = "性别不能为空";
        }
        if (StrUtil.isBlank(daDaVO.getBank())) {
            msg = "账号不能为空";
        }
        if (StrUtil.isBlank(daDaVO.getPasWord())) {
            msg = "密码不能为空";
        }
        if (StrUtil.isBlank(daDaVO.getTel())) {
            msg = "电话不能为空";
        }
        return msg;
    }
}
